<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="活动名称" prop="actName">
        <el-input
          v-model="queryParams.actName"
          placeholder="请输入活动名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="上架状态" prop="publishSt">
        <el-select
          v-model="queryParams.publishSt"
          placeholder="上架状态"
          clearable
        >
          <el-option
            v-for="dict in dict.type.bus_activity_publish_st"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="审批状态" prop="appSt">
        <el-select v-model="queryParams.appSt" placeholder="审批状态" clearable>
          <el-option
            v-for="dict in dict.type.bus_activity_audit_st"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="dateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        >
        </el-date-picker>
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['activity:info:add']"
          >新增</el-button
        >
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['activity:info:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['activity:info:remove']"
          >删除</el-button
        >
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['activity:info:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="infoList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column v-if="false" align="center" prop="id" />
      <el-table-column
        label="活动名称"
        width="300"
        align="center"
        prop="actName"
      />
      <!-- <el-table-column label="活动说明" align="center" prop="actDesc" /> -->
      <el-table-column label="上架状态" align="center" prop="publishSt">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.bus_activity_publish_st"
            :value="scope.row.publishSt"
          />
        </template>
      </el-table-column>
      <!-- <el-table-column label="活动说明" align="center" prop="actDesc" /> -->
      <el-table-column label="审批状态" align="center" prop="appSt">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.bus_activity_audit_st"
            :value="scope.row.appSt"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="创建时间"
        align="center"
        prop="crtTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.crtTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建人" align="center" prop="crtBy" />
      <el-table-column
        label="更新时间"
        align="center"
        prop="updTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新人" align="center" prop="updBy" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            v-if="scope.row.publishSt === 0 && scope.row.appSt !== 10"
            icon="el-icon-edit"
            @click="handlePublish(scope.row)"
            v-hasPermi="['activity:info:edit']"
            >上架</el-button
          >
          <el-button
            size="mini"
            type="text"
            v-if="scope.row.publishSt === 1 && scope.row.appSt !== 20"
            icon="el-icon-edit"
            @click="handlePublish(scope.row)"
            v-hasPermi="['activity:info:edit']"
            >下架</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-info"
            @click="handleDetail(scope.row)"
            v-hasPermi="['activity:info:edit']"
            >详情</el-button
          >
          <el-button
            size="mini"
            type="text"
            v-if="scope.row.publishSt === 0 && scope.row.appSt !== 10"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['activity:info:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            v-if="scope.row.publishSt === 0 && scope.row.appSt !== 10"
            @click="handleDelete(scope.row)"
            v-hasPermi="['activity:info:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改活动对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="活动名称" prop="actName">
          <el-input v-model="form.actName" placeholder="请输入活动名称" />
        </el-form-item>
        <el-form-item label="活动说明" prop="actDesc">
          <el-input
            v-model="form.actDesc"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
        <el-form-item label="优惠信息" prop="actDesc">
          <el-button v-if="!this.readOnly" type="primary" @click="subAdd"
            >+</el-button
          >
        </el-form-item>
        <!-- //我将我的代码简化到最简，和工程相关的已经删除，启发思路即可 -->
        <div v-for="(item, index) in form.arrayData" :key="item.id">
          <!-- //上面这个是关键代码，所有思路靠这个打通 -->
          满
          <el-input
            type="number"
            style="width: 120px; margin: 8px"
            placeholder="起算金额"
            v-model="item.targetAmt"
          >
          </el-input>
          元，优惠
          <el-input
            style="width: 120px; margin: 8px"
            type="number"
            placeholder="优惠金额"
            v-model="item.reduceAmt"
          >
          </el-input>
          元

          <a
            v-if="!this.readOnly"
            style="padding-left: 20px"
            @click="subDelete(item, index)"
            ><font color="blue">删除</font></a
          >
        </div>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button v-if="!this.readOnly" type="primary" @click="submitForm"
          >确 定</el-button
        >
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listInfo,
  getInfo,
  delInfo,
  addInfo,
  publish,
  updateInfo,
} from "@/api/business/activityList";

export default {
  name: "Info",
  dicts: ["bus_activity_audit_st", "bus_activity_publish_st"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 活动表格数据
      infoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      //修改框只读
      readOnly: false,
      // 日期范围
      dateRange: [],

      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        actName: null,
        actDesc: null,
        appSt: null,
        crtTime: null,
        crtBy: null,
        updTime: null,
        updBy: null,
        status: null,
      },
      // 表单参数
      form: {
        arrayData: [
          {
            id: 1,
            targetAmt: 500,
            reduceAmt: 100,
          },
        ],
        dataNum: 0,
      },
      // 表单校验
      rules: {
        actName: [
          { required: true, message: "活动名称不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    subDelete(item, index) {
      if (this.form.arrayData.length <= 1) {
        //如果只有一个输入框则不可以删除
        alert("活动必须至少有1个满减优惠信息！");
        return false;
      }
      this.form.arrayData.splice(index, 1); //删除了数组中对应的数据也就将这个位置的输入框删除
    },
    subAdd(item) {
      console.log(this.form.arrayData);
      this.form.arrayData.push(
        //增加就push进数组一个新值
        {
          id: this.form.dataNum++,
          targetAmt: 0,
          reduceAmt: 0,
        }
      );
    },
    /** 查询活动列表 */
    getList() {
      this.loading = true;
      listInfo(this.addDateRange(this.queryParams, this.dateRange)).then(
        (response) => {
          this.infoList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.readOnly=false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        actName: null,
        actDesc: null,
        appSt: null,
        delFlag: null,
        crtTime: null,
        crtBy: null,
        updTime: null,
        updBy: null,
        arrayData: [],
        dataNum: 0,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.readOnly = false;
      this.reset();
      this.open = true;
      this.title = "添加活动";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getInfo(id).then((response) => {
        this.readOnly = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改活动";
      });
    },
    /** 详情按钮操作 */
    handleDetail(row) {
      this.reset();
      const id = row.id || this.ids;
      getInfo(id).then((response) => {
        this.readOnly = true;
        this.form = response.data;
        this.open = true;
        this.title = "活动详情";
      });
    },

    /** 上下架 */
    handlePublish(row) {
      const that = this;
      this.$modal.confirm("是否提交审批？").then(function () {
        // confirm("是否提交审批？", "提示", {
        //   confirmButtonText: "确定",
        //   cancelButtonText: "取消",
        //   type: "warning",
        // }).then(() => {
        that.reset();
        const id = row.id || that.ids;
        publish(id)
          .then((response) => {
            that.$modal.msgSuccess("提交审批成功");
            that.getList();
          })
          .catch((res) => {
            that.$modal.message(res);
          });
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateInfo(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInfo(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除活动编号为"' + ids + '"的数据项？')
        .then(function () {
          return delInfo(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "activity/info/export",
        {
          ...this.queryParams,
        },
        `info_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
